package com.bluestone.extension.totalvalidator;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;

import com.totalvalidator.Cancellor;
import com.totalvalidator.exceptions.CancelledException;
import com.totalvalidator.parser.CommandLineParser;
import com.totalvalidator.parser.TagParser;
import com.totalvalidator.reports.MainReportPage;
import com.totalvalidator.reports.TagReport;
import com.totalvalidator.sgml.EntityEncoder;
import com.totalvalidator.utils.FileUtils;
import com.totalvalidator.utils.MyReader;
import com.totalvalidator.validations.AccessibilityValidation;

public class Section508Validation implements Cancellor{

	public static void main(String[] args) {
		try{
			Section508Validation validator = Section508Validation.getSection508Validation();
			validator.validate("E:\\workspace\\luster\\trunk\\section508\\pages\\www.parosproxy.org\\page1271598770093.html");
		}catch(Exception me){
			me.printStackTrace();
		}  


	}
	private static Section508Validation validator= null;
	
	private Section508Validation(){
	       commandlineparser = new CommandLineParser();
	       commandlineparser.setAccessibility("508");	
	       tagparser = new TagParser();
	       fileUtils = new FileUtils();
		   String [] args0 = new String[2];;
		   args0[0] = "-path";
		   args0[1] = ".\\section508\\";
		   fileUtils.checkInstallationFolder(args0);	
		   defaultEncoder = new EntityEncoder();
		   accessibilityvalidation = new AccessibilityValidation(commandlineparser.getAccessibilityLevel(), null, null, commandlineparser.isUS508(), commandlineparser.isWCAG2(), "", defaultEncoder);		  
		   mainreportpage = new MainReportPage(null, ".\\results", ".\\section508\\summary.html", false);
		   new File(".\\section508\\results").mkdirs();
	}
	public static Section508Validation getSection508Validation(){
		if(validator == null){
			validator = new Section508Validation();
		}
		return validator;
	}
	
	private CommandLineParser commandlineparser = null;
	private TagParser tagparser = null;
	private FileUtils fileUtils = null;
	private EntityEncoder defaultEncoder =null;
	private AccessibilityValidation accessibilityvalidation =null;
	private MainReportPage mainreportpage = null;
    private int totalErrors = 0;
    private int totalWarnings = 0;
	public void validate(String file){
		try{
		   TagReport tagreport = new TagReport();
		   MyReader reader = new MyReader();    
		   File f = new File(file);
	       FileInputStream in = new FileInputStream(f);
		   reader.setInputStream(in);
		   reader.setContentType("text/html");
		   List tags = tagparser.parseFile(reader, false, null, false, fileUtils, commandlineparser, defaultEncoder);           
           accessibilityvalidation.validate(tags, this);	                      
           String results = tagreport.format(tags, file, mainreportpage, commandlineparser, 0, null, null, defaultEncoder, null, false, false, false);
           String subPage = "";

           
           if(results != null)
           {
               subPage = tagreport.saveResults(".\\section508\\results");
               mainreportpage.reportPage(subPage);
               totalErrors += tagreport.getErrorTotal();
               totalWarnings += tagreport.getWarningsTotal();             
           } 
           
           mainreportpage.close(0, 0, totalErrors,totalWarnings, commandlineparser.getOptions(), null, false);
           
	       
		}catch(Exception me){
			me.printStackTrace();
		}
	}

	public void isCancelled() throws CancelledException {
		// TODO Auto-generated method stub
		
	}
}
